package com.example.bbs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.bbs.entity.Disease;
import com.example.bbs.entity.DiseaseTag;
import com.example.bbs.entity.Selfchecking;
import com.example.bbs.mapper.DiseaseMapper;
import com.example.bbs.mapper.DiseaseTagMapper;
import com.example.bbs.mapper.SelfcheckingMapper;
import com.example.bbs.service.DiseaseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.bbs.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author jiaziyun
 * @since 2021-04-30
 */
@Service
public class DiseaseServiceImpl extends ServiceImpl<DiseaseMapper, Disease> implements DiseaseService {
    @Autowired
    private DiseaseMapper diseaseMapper;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Override
    public List<Disease> selectParts() {
        QueryWrapper<Disease> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id", "title", "introduce", "view", "date");
        return diseaseMapper.selectList(queryWrapper);
    }

    @Override
    public Disease selectPartsById(int id) {
        QueryWrapper<Disease> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id", "title", "introduce", "view", "date");
        if (id == 0) {
            return diseaseMapper.selectList(queryWrapper).get(0);
        }
        queryWrapper.eq("id", id);
        return diseaseMapper.selectList(queryWrapper).get(0);
    }

    @Override
    public Disease selectDiseaseById(int id) {
        //时时更新数据库
        //int view = diseaseMapper.getViewById(id);
        //HashMap<String, Integer> temp = new HashMap<>();
        //temp.put("id",id);
        //temp.put("view",view+1);
        //diseaseMapper.updateViewById(temp);
        //redis缓存
        String viewkey = "view_" + id;
        redisTemplate.opsForValue().increment(viewkey, 1);
        System.out.println("redis； viewid= " + viewkey);
        return diseaseMapper.selectById(id);
    }
}
